package org.keyroy.map_editor.ui.panels;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import org.keyroy.map_editor.map.m.Rect;
import org.keyroy.map_editor.ui.listeners.PointerListener;
import org.keyroy.map_editor.ui.panels.MapPanel.MapEvent;

public class MapThumbPanel extends JPanel implements MapEvent {
	public static final Color RECT_COLOR = new Color(0xffff6600, false);

	private static final long serialVersionUID = 1L;
	private BufferedImage bufferedImage;
	private Rect rect = new Rect();
	private double scale;
	private MapPanel mapPanel;

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (bufferedImage != null) {
			int ox = (getWidth() - bufferedImage.getWidth()) / 2;
			int oy = (getHeight() - bufferedImage.getHeight()) / 2;
			g.drawImage(bufferedImage, ox, oy, null);
			g.setColor(RECT_COLOR);
			g.drawRect(rect.x + ox, rect.y + oy, rect.width, rect.height);
		}
	}

	@Override
	public void onBinding(MapPanel mapPanel) {
		this.mapPanel = mapPanel;
		PointerListener listener = new PointerListener() {
			@Override
			public void mousePressed(MouseEvent e) {
				int ox = (getWidth() - bufferedImage.getWidth()) / 2;
				int oy = (getHeight() - bufferedImage.getHeight()) / 2;

				int px = e.getX() - ox;
				int py = e.getY() - oy;

				float sx = (float) px / (float) bufferedImage.getWidth();
				float sy = (float) py / (float) bufferedImage.getHeight();

				MapThumbPanel.this.mapPanel.setCenter(sx, sy);
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				mousePressed(e);
			}
		};
		addMouseListener(listener);
		addMouseMotionListener(listener);
		onElementChange();
	}

	@Override
	public void onElementChange() {
		copyMap(mapPanel);
		computeArea(mapPanel);
		repaint();
		mapPanel.repaint();
	}

	@Override
	public void onSelectedZoneChange() {
		computeArea(mapPanel);
		repaint();
	}

	private final void computeArea(MapPanel mapPanel) {
		int rx = (int) ((double) (mapPanel.mapCenter.x - mapPanel.getWidth() / 2) * scale);
		int ry = (int) ((double) (mapPanel.mapCenter.y - mapPanel.getHeight() / 2) * scale);

		int rw = (int) ((double) mapPanel.getWidth() * scale);
		int rh = (int) ((double) mapPanel.getHeight() * scale);

		rect.setLocation(rx, ry);
		rect.setSize(rw, rh);
	}

	private final void copyMap(MapPanel mapPanel) {

		double xScale = ((double) getWidth()) / ((double) mapPanel.map.getPWidth());
		double yScale = ((double) getHeight()) / ((double) mapPanel.map.getPHeight());

		int iWidth = getWidth(), iHeight = getHeight();
		if (xScale > yScale) {
			scale = yScale;
			iWidth = (int) ((double) mapPanel.map.getPWidth() * scale);
		} else if (xScale < yScale) {
			scale = xScale;
			iHeight = (int) ((double) mapPanel.map.getPHeight() * scale);
		} else {
			scale = xScale;
		}

		if (bufferedImage == null || bufferedImage.getWidth() != iWidth || bufferedImage.getHeight() != iHeight) {
			bufferedImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
		}

		Graphics2D g2d = bufferedImage.createGraphics();
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, iWidth, iHeight);
		g2d.scale(scale, scale);
		mapPanel.copyAll(g2d);
		g2d.dispose();
	}
}
